package com.SouthernWall_404.age_tool.common.world.Entity;

import com.SouthernWall_404.age_tool.common.API.Capability.Provider.TriggersCapProvider;
import com.SouthernWall_404.age_tool.common.API.Capability.TriggersCap;
import com.SouthernWall_404.age_tool.common.Network.ModChannels;
import com.SouthernWall_404.age_tool.common.Network.Packets.S2C.MimicBlockRespondS2CPacket;
import com.SouthernWall_404.age_tool.common.init.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

public class TriggerBlockEntity  extends MimicBlockEntity implements MenuProvider {
    public boolean isTriggered;
    public boolean isPressed =false;
    public int pressed_time=0;
    public CompoundTag triggerPack=new CompoundTag();
    public UUID remoteId=UUID.randomUUID();

    public static final String TRIGGERPACK_TAG="trigger";
    public static final String ID_TAG="uuid";

    public TriggerBlockEntity(BlockEntityType<?> blockEntityType, BlockPos blockPos, BlockState blockState) {
        super(blockEntityType, blockPos, blockState);
    }



    public TriggerBlockEntity(BlockPos pos, BlockState blockState) {
        super(ModBlockEntities.TRIGGER_BLOCK_ENTITY.get(), pos, blockState);
    }

    public @Nullable TriggersCap getTriggers()
    {
        if(!level.isClientSide&&level!=null)
        {
            return getLevel().getCapability(TriggersCapProvider.TRIGGERSCAP).orElse(null);
        }
        else return null;

    }
    @Override
    public void setChanged() {
        super.setChanged();

        // 当数据更改时，同步到客户端
        if (level != null && !level.isClientSide) {
            Level serverLevel = (Level) level;
            if (serverLevel instanceof ServerLevel) {
                // 通知客户端更新
                CompoundTag updateTag = getUpdateTag();
                MimicBlockRespondS2CPacket packet = new MimicBlockRespondS2CPacket(updateTag, getBlockPos());

                // 发送给所有正在追踪此区块的玩家
                for (ServerPlayer player : ((ServerLevel) serverLevel).players()) {
                    if (player.distanceToSqr(getBlockPos().getX(), getBlockPos().getY(), getBlockPos().getZ()) < 64 * 64) {
                        ModChannels.sendToClient(packet, player);
                    }
                }
            }
        }
    }

    @Override
    public CompoundTag getUpdateTag() {
        CompoundTag tag = super.getUpdateTag();
        saveAdditional(tag); // 确保所有数据都已保存
        return tag;
    }

    public void addUUID()
    {
        TriggersCap triggersCap=getTriggers();
        if(triggersCap!=null)
        {
            triggersCap.addRemote(this.remoteId);
        }
    }

    @Override
    public Component getDisplayName() {
        return null;
    }

    @Override
    public @Nullable AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) {
        return null;
    }


    @Override
    public void load(CompoundTag nbt) {
        super.load(nbt);
        loadTriggerPack();
        loadRemoteId();
    }

    @Override
    protected void saveAdditional(CompoundTag nbt) {
        saveRemoteId();
        saveTriggerPack();
        super.saveAdditional(nbt);
    }

    public void loadTriggerPack()
    {
        this.triggerPack=new CompoundTag();
        if(this.modPack.contains(TRIGGERPACK_TAG))
        {
            this.triggerPack=modPack.getCompound(TRIGGERPACK_TAG);
        }
    }
    public void loadRemoteId()
    {
        remoteId =UUID.randomUUID();
        if(this.triggerPack.contains(ID_TAG))
        {
            remoteId =this.triggerPack.getUUID(ID_TAG);
        }
    }

    public void saveTriggerPack()
    {
        this.modPack.put(TRIGGERPACK_TAG,triggerPack);
    }
    public void saveRemoteId()
    {
        this.triggerPack.putUUID(ID_TAG,remoteId);
    }
}
